<template>
  <BaseChatMsg :avatar="avatar" :align="align">
    <template v-slot:chatcontent>
      <div class="audio" @click="play">
        <span>{{ audioDuration }}</span>
        <svg-icon class="icon" icon="wifi"></svg-icon>
        <audio :src="url" ref="audio"></audio>
      </div>
    </template>
  </BaseChatMsg>
</template>

<script>
import BaseChatMsg from './BaseChatMsg.vue'
export default {
  name: 'ChatMsgAudio',
  components: { BaseChatMsg },
  props: {
    avatar: String,
    url: String,
    duration: [Number, String],
    align: {
      type: String,
      validator: value => {
        return ['left', 'right'].includes(value)
      }
    }
  },
  data() {
    return {
    }
  },
  computed: {
    audioDuration() {
      const time = Math.ceil(Number(this.duration) / 1000)
      return time ? time + "''" : ''
    }
  },
  methods: {
    play() {
      const audio = this.$refs.audio
      if (!audio.currentTime) {
        audio.play()
      } else {
        audio.pause()
        audio.currentTime = 0
      }
    },
    getAudioTime() {
      const audio = this.$refs.audio
      if (audio) {
        Math.ceil(audio.duration)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.audio {
  width: 100px;
  text-align: right;
  color: $gray-7;
  .icon {
    transform: rotate(-90deg);
  }
}
</style>
